package com.springboot.dwhy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.dwhy.entity.Anbaoyuan;
import com.springboot.dwhy.entity.Youke;
import com.springboot.dwhy.mapper.YoukeMapper;
import com.springboot.dwhy.qpage.QPage;
import com.springboot.dwhy.service.IYoukeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.dwhy.util.Result;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-02-22
 */
@Service
public class YoukeServiceImpl extends ServiceImpl<YoukeMapper, Youke> implements IYoukeService {

    /**
     * 分页查询分类
     * @param qPage
     * @return
     */
    @Override
    public IPage getPageList(QPage qPage, Youke obj) {
        IPage iPage = new Page(qPage.getPage(), qPage.getRows());
        QueryWrapper<Youke> queryWrapper=new QueryWrapper<Youke>();
        if(!StringUtils.isEmpty(obj.getName())){
            queryWrapper.like("name",obj.getName());
        }
        queryWrapper.orderByDesc("ctime");
        IPage mapList =this.page(iPage,queryWrapper);
        return mapList;
    }

    /**
     * 新增
     * @param obj
     * @return
     */
    @Override
    public Result insert(Youke obj) {
        QueryWrapper<Youke> queryWrapper=new QueryWrapper<Youke>();
        queryWrapper.eq("name",obj.getName());
        if(this.getOne(queryWrapper)!=null){
            return Result.reError("此名称已经存在，请重新取名");
        }
        obj.setCtime(new Date());
        if(this.save(obj)){
            return Result.reOk("恭喜，新增成功");
        }
        return Result.reError("新增出错");
    }

    /**
     * 修改
     * @param obj
     * @return
     */
    @Override
    public Result updates(Youke obj) {
        QueryWrapper<Youke> queryWrapper=new QueryWrapper<Youke>();
        queryWrapper.eq("name",obj.getName());
        Youke newrole=this.getOne(queryWrapper);
        if(newrole!=null&&newrole.getId()!=obj.getId()){
            return Result.reError("此名称已经存在，请重新取名");
        }
        obj.setUtime(new Date());
        if(this.updateById(obj)){
            return Result.reOk("恭喜，修改成功");
        }
        return Result.reError("修改出错");
    }

}
